package com.hqyj.javaSpringBoot.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.common.entiy.ResultEntity;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.City;
import com.hqyj.javaSpringBoot.modules.test.service.CityService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 127.0.0.1/api/city   https:443 && http:80 ----post
     * 增加城市信息
     * 接收json提交方式，通过@RequestBody
     *  {"cityName":"dreamCity", "localCityName":"梦想城市", "countryId":522,
     *      * "district":"sichuan", "population":1780000}
     */
    @PostMapping(value = "/city",consumes = "application/json")
    public ResultEntity<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    };

    /**
     * 127.0.0.1/api/city -----put
     * 修改城市信息
     * 接收表单提交方式，通过@ModelAttribute
     * cityId=2257,cityName=aaaaa,district=bbbbbb
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public ResultEntity<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }


    /**
     * 127.0.0.1/api/city/2260 -----put
     * 删除城市信息
     * 接收url表单中拼接的数据，通过@PathVariable
     * cityId=2260
     */
    @DeleteMapping(value = "/city/{cityId}")
    public ResultEntity<City> deleteCityByCityId(@PathVariable int cityId){
        return cityService.deleteCityByCityId(cityId);
    }

    /**
     * 127.0.0.1/api/city/2259 -----get
     * 根据城市id查询城市信息
     * 接收url表单中拼接的数据，通过@PathVariable
     * cityId=2259
     */
    @GetMapping(value = "/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId) {
        return cityService.getCityByCityId(cityId);
    }

    /**
     * 127.0.0.1/api/cities/522 -----get
     * 根据城市id查询城市信息
     * 接收url表单中拼接的数据，通过@PathVariable
     * countryId=522
     */
    @GetMapping(value = "cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId){
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * 127.0.0.1/api/cities -----post
     * 模糊查询分页城市信息
     * 接收json数据,通过@RequestBody
     * {"currentPage":1, "pageSize":5, "orderBy":"city_id",
     *      * "direction":"desc", "keyword": "ch"}
     */
    @PostMapping(value = "/cities",consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean){
        return cityService.getCitiesBySearchBean(searchBean);
    }

}
